//
//  CrashFilter.swift
//  swiftDemo
//
//  Created by JIENING ZHANG on 2023/11/26.
//  Copyright © 2023 lovivid. All rights reserved.
//

import Foundation
class CrashFilter: NSObject {
    class func demoLogic() {
        let manager = FileManager.default
        
        var b386Arr : [[AnyHashable:Any]] = []
        
        if let tjsonLocalUrl = Bundle.main.url(forResource: "t", withExtension: "json") {
            
            let readHandler = try? FileHandle(forReadingFrom:tjsonLocalUrl)
            if readHandler != nil {
                let data = readHandler!.readDataToEndOfFile()
                //let readString = String(data: data, encoding: String.Encoding.utf8)
                
                let jsonStr = String(data: data, encoding: String.Encoding.utf8)
                
                if jsonStr == nil {
                    //print("readFromJson, fail to read json string")
                    return;
                }
                
                //var rpstr = jsonStr!.replacingOccurrences(of: "\\n", with: "")
                //var rpstr2 = rpstr.replacingOccurrences(of: "\\\"", with: "\"")
                
                //print("readFromJson, jsonStr: \(jsonStr!)")
                
                //let jsonObj = jsonStr!.ggJsonObj();
                //JSONSerialization.jsonObject(with: utf8Data!, options: .allowFragments)
                
                if let jsonObj = jsonStr!.ggJsonObj(){ //try? JSONSerialization.jsonObject(with: data, options: .allowFragments) {
                    
                    if let jsonArr = jsonObj as? Array<Any> {
                        for jsonobj in jsonArr {
                            // 过滤 CFBundleVersion = 386 的 crash 记录
                            if let dic = jsonobj as? [AnyHashable:Any] {
                                if let sysInfo = dic["system"] as? [AnyHashable:Any] {
                                    if let bversion = sysInfo["CFBundleVersion"] as? String {
                                        if bversion == "386" {
                                            
                                            var dicCopy:[AnyHashable:Any] = dic
                                            if let crashInfo = dicCopy["crash"] as? [AnyHashable:Any] {
                                                if let threadArr = crashInfo["threads"] as? [[AnyHashable:Any]] {
                                                    for tinfo in threadArr {
                                                        // 过滤 crashed = true 的 threads 记录
                                                        
                                                        if let crashed = tinfo["crashed"] as? Int {
                                                            if crashed > 0 {
                                                                var crashInfoCopy:[AnyHashable:Any] = crashInfo
                                                                crashInfoCopy.removeValue(forKey: "threads")
                                                                crashInfoCopy["crashedThread"] = tinfo
                                                                dicCopy["crash"] = crashInfoCopy;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            
                                            b386Arr.append(dicCopy)
                                            
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                
                //print("textFileRead,FileHandle: \(readString ?? "")")
            } else {
                print("textFileRead,FileHandle: fail to create FileHandle")
            }
        }
        
        // 使用 JSONSerialization 将字典对象变成 json 字符串的 category
        let jsonString = b386Arr.ggJsonStr()
        
        if jsonString == nil {
            print("writeJson: 不能表示为json")
            return;
        }
        
        let tempPath=NSTemporaryDirectory();
        let jsonPath = tempPath.appending("/t386.json")
        print("writeJson : jsonPath:\(jsonPath)")
        
        do {
            try jsonString!.write(toFile: jsonPath, atomically: true, encoding: String.Encoding.utf8)
            //try poem.write(to:URL(fileURLWithPath:path), atomically:true, encoding:String.Encoding.utf8)
            print("writeJson:succ write:\(jsonPath)")
        } catch {
            print("writeJson:fail write:\(jsonPath)")
        }
    }
}

